<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>移动身体</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }

        #main {
            position: relative;
            border: 1px solid #ccc;
            width: 300px;
            height: 350px;
            margin: 0px auto;
            background-color: #ccc;
        }

        #stage {
            background-color: white;
            width: 300px;
            height: 300px;
        }

        #control {
            background-color: white;
            border: 1px solid #ccc;
            text-align: center;
            height: 25px;
        }

        button {
            margin: 0px 10px;
        }

        #food {
            position: absolute;
            width: 15px;
            height: 15px;
            border: 1px solid red;
        }

        #snake>div {
            position: absolute;
            width: 15px;
            height: 15px;
            border: 1px solid black;
        }
    </style>
</head>

<body>
    <div id="main">
        <div id="stage">
            <div id="snake">
                <div style="top: 60px;left: 30px;">*</div>
                <div style="top: 45px;left: 30px;"></div>
                <div style="top: 30px;left: 30px;"></div>
            </div>
            <div style="left: 15px;" id="food"></div>
        </div>
        <div id="control">
            <button id="start">start</button>
            <button id="end">end</button>

        </div>
        <div id="info">11</div>
    </div>
    <script>
        start.onclick = function () {
            createFood()
        }
        function createFood() {
            var x = parseInt(Math.random() * 20) * 15
            var y = parseInt(Math.random() * 20) * 15
            food.style.left = x + "px"
            food.style.top = y + "px"
        }
        function moveSnake(direct) {
            var offsetX = 0
            var offsetY = 0
            var head = snake.children[0]
            //移动身体 蓄力 
            var len = snake.children.length
            for (var i = len - 1; i > 0; i--) {
                snake.children[i].style.left = snake.children[i - 1].style.left
                snake.children[i].style.top = snake.children[i - 1].style.top
            }
            switch (direct) {
                case "up":
                    offsetX = 0
                    offsetY = -15
                    break;
                case "down":
                    offsetX = 0
                    offsetY = 15
                    break;
                case "left":
                    offsetX = -15
                    offsetY = 0
                    break;
                case "right":
                    offsetX = 15
                    offsetY = 0
                    break;
                default:
                    break;
            }

            head.style.left = parseInt(head.style.left) + offsetX + "px"
            head.style.top = parseInt(head.style.top) + offsetY + "px"
            //移动身体


        }

        moveSnake("down")
        moveSnake("down")
        moveSnake("left")
    </script>
</body>

</html>